﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class ManagerSetForm : Form
    {
        private BLL.ManagerService managerService = new BLL.ManagerService();
        public ManagerSetForm()
        {
            InitializeComponent();
        }

        public void dataTableInit()
        {
            manager_grid_view.DataSource = managerService.list(new Admin());
        }
        private void ManagerSetForm_Load(object sender, EventArgs e)
        {
            dataTableInit();
        }

        // 新增管理员
        private void add_manager_btn_Click(object sender, EventArgs e)
        {
            Admin admin = new Admin(GetTimeStamp(),
                manager_person_name.Text,
                manager_username.Text,
                manager_password.Text,
                manager_email.Text,
                0 != manager_is_admin.CheckState ? "是" : "否");

            // 用户名不能重复

            Admin temp = new Admin();
            temp.UserName = manager_username.Text;
            List<Admin> list = managerService.list(temp);
            if (list.Count > 0)
            {
                MessageBox.Show("该用户名已存在！");
                return;
            }
            bool flag = managerService.save(admin);
            if (flag)
            {
                MessageBox.Show("添加成功！");
                resetForm();
                dataTableInit();
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
        }
        public string GetTimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }


        // 右击 删除事件
        private void managerStripMenuItem_Click(object sender, EventArgs e)
        {
            // 获取选定的元素
            Console.WriteLine("删除事件");
            Admin admin = (Admin)manager_grid_view.CurrentRow.DataBoundItem;
            bool flag = managerService.deleteById(admin.PersonID);
            if (flag)
            {
                MessageBox.Show("删除成功！");
                dataTableInit();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        // 右击 设置管理员事件
        private void managerSetStripMenuItem_Click(object sender, EventArgs e)
        {
            // 获取选定的元素
            Console.WriteLine("设置管理员事件");
            Admin admin = (Admin)manager_grid_view.CurrentRow.DataBoundItem;
            bool flag = managerService.setAdmin(admin);
            if (flag)
            {
                MessageBox.Show("设置成功！");
                dataTableInit();
            }
            else
            {
                MessageBox.Show("设置失败！");
            }
        }

        // 清空text
        private void resetForm()
        {
            var list = manager_group.Controls;
            foreach (Control a in list)
            {
                if (a is TextBox)
                    a.Text = "";
            }
        }


    }
}
